package com.sdgakj.utils;

import java.util.Random;

import org.springside.modules.security.utils.Digests;
import org.springside.modules.utils.Encodes;

public class PasswordUtil {

	public static final String HASH_ALGORITHM = "SHA-1";
    public static final int HASH_INTERATIONS = 1024;
    private static final int SALT_SIZE = 8;
    
    /**
     * 生成6位数字的密码
     * 
     * @param userDao
     */
    public static String getAuto6Password() {
        Random random = new Random();
        String sRand = "";
        for(int i = 0; i < 6; i++)
        {
            String rand = String.valueOf(random.nextInt(10));
            sRand += rand;
        }
        return sRand;
    }
    
    
    /**
     * 生成salt
     */
    public static byte[] genSalt() {
        return Digests.generateSalt(SALT_SIZE);
    }
    
    /**
     * 返回salt和password
     * 
     * @param plainPassword  明文密码
     * @return
     */
    public static String[] getSaltAndPwd(String plainPassword) {
    	byte[] salt = Digests.generateSalt(SALT_SIZE);
    	String saltStr = Encodes.encodeHex(salt);
    	byte[] hashPassword = Digests.sha1(plainPassword.getBytes(), salt, HASH_INTERATIONS);
    	String pwdStr = Encodes.encodeHex(hashPassword);
    	return new String[]{saltStr,pwdStr};
    }
    
}
